package com.nantian.nfcm.bms.loan.web;

import com.nantian.nfcm.bms.loan.entity.LoanJournal;
import com.nantian.nfcm.bms.loan.service.LoanJournalService;
import com.nantian.nfcm.util.vo.GridData;
import com.nantian.nfcm.util.vo.ResultInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/loanJournal")
public class LoanJournalController {
	private static Logger log = LoggerFactory.getLogger(LoanJournalController.class);
	private LoanJournalService loanJournalService;

	@Autowired
	public LoanJournalController(LoanJournalService loanJournalService) {
		this.loanJournalService = loanJournalService;
	}

	@RequestMapping("/findByLoanId")
	@ResponseBody
	private ResultInfo findByLoanId(String loanId) {
		ResultInfo resultInfo = new ResultInfo();
		try {
			List<LoanJournal> loanJournals = loanJournalService.findByLoanId(loanId);
			resultInfo.setData(loanJournals);
			resultInfo.setSuccess("true");
		} catch (Exception e) {
			log.error(e.getMessage());
			resultInfo.setSuccess("false");
			resultInfo.setData(e.getMessage());
		}
		return resultInfo;
	}

	/**
	 * 查询手机端的RoadMap
	 * @param loanId
	 * @return
	 */
	@RequestMapping("/findByLoanId4Mobile")
	@ResponseBody
	private ResultInfo findByLoanId4Mobile(String loanId) {
		ResultInfo resultInfo = new ResultInfo();
		try {
			List<LoanJournal> loanJournals = loanJournalService.findByLoanId(loanId);

			String htmls = "<article>";
			String nowYear = "";
			if (loanJournals != null && loanJournals.size() > 0) {
				for (int i = 0; i < loanJournals.size(); i++) {

					LoanJournal loanJournal = loanJournals.get(i);

					if (!nowYear.equals(loanJournal.getCurProcessUpdateYear())) {
						nowYear = loanJournal.getCurProcessUpdateYear();
						htmls += "<h3>" + nowYear + "年</h3>";
					}

					htmls += "<section> <span class=\"point-time point-blue\"></span>"
							+ "<time datetime=\"\"> <span>"
							+ loanJournal.getCurProcessUpdateDate()
							+ "</span> </time><aside> <p class=\"things\" >"
							+ loanJournal.getRemark() + "</p> </aside> </section>";
				}
			} else {

				htmls += "<h4>无历史记录！</h4>";
			}

			htmls += "</article>";

			resultInfo.setData(htmls);
			resultInfo.setSuccess("true");
		} catch (Exception e) {
			log.error(e.getMessage());
			resultInfo.setSuccess("false");
			resultInfo.setData(e.getMessage());
		}
		return resultInfo;
	}
}
